# -*- coding: utf-8 -*-
# @Author : zhaochen
# @Date   : 2019/6/4
# @File   : IC26_WhiteUser.py
# @E-mail : zhaochen@bbdservice.com

'''eof
name:是否为个人征信白户
code:IC26_WhiteUser
tableName:
columnName:
groups:个人征信模块
dependencies:IC_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''

import sys


reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def getTable(table_Name):
    try:
        data = IC_DSJ['data'].get(table_Name)
        if data in null_type_list:
            return None
        else:
            # 存在1条为空的数据[{}]
            if isinstance(data, list) and len(data) == 1 and data[0]['reportNo'] in null_type_list:
                return [{}]
            # 存在空list []
            elif isinstance(data, list) and len(data) == 0:
                return None
            else:
                return data
    except:
        return None

def IC26_WhiteUser():
    '''1.身份信息表中，生日、婚姻状况、学历、注册地址全为空
        2.信用提示表中，首笔贷款发放月份、首张贷记卡发卡月份、首张准贷记卡发卡月份全为空
        若上述2条任一满足则为个人征信白户'''

    try:
        ICRIdentity = getTable('ICRIdentity')
        # 若表异常 认为白户
        if ICRIdentity is None or ICRIdentity == [{}]:
            Factor1 = 1
        else:
            birthday = ICRIdentity[0].get('birthday')
            eduLevel = ICRIdentity[0].get('eduLevel')
            maritalState = ICRIdentity[0].get('maritalState')
            registeredAddress = ICRIdentity[0].get('registeredAddress')

            if birthday in null_type_list and eduLevel in null_type_list and maritalState in null_type_list\
                and registeredAddress in null_type_list:
                Factor1 = 1
            else:
                Factor1 = 0
    except:
        Factor1 = 1


    try:
        ICRCreditCue002 = getTable('ICRCreditCue002')
        # 若表异常 认为白户
        if ICRCreditCue002 is None or ICRCreditCue002 == [{}]:
            Factor2 = 1
        else:
            firstLoanOpenMonth = ICRCreditCue002[0].get('firstLoanOpenMonth')
            firstLoancardOpenMonth = ICRCreditCue002[0].get('firstLoancardOpenMonth')
            firstStandardLoancardOpenMonth = ICRCreditCue002[0].get('firstStandardLoancardOpenMonth')

            if firstLoancardOpenMonth in null_type_list and firstLoanOpenMonth in null_type_list \
                and firstStandardLoancardOpenMonth in null_type_list:
                Factor2 = 1
            else:
                Factor2 = 0
    except:
        Factor2 = 1
    # 由于plutus平台out指标 只有当返回缺失值时 才触发  故当为白户时 返回缺失值
    if Factor1 or Factor2:
        return u'缺失值'
    else:
        return 0



result = IC26_WhiteUser()
